/**
 * 
 */
package systole.view.crud.medicine;

import java.awt.Frame;
import java.util.Calendar;
import javax.swing.JDialog;
import systole.domain.clinicalInformation.Medicine;
import systole.exceptions.ExceptionDAO;
import systole.utils.ImageUtils;
import systole.view.crud.controller.ControllerEntityEdition;

/**
 * @author jmj
 *
 */
public class ControllerMedicineEdition extends ControllerEntityEdition {

    /**
     * 
     */
    public ControllerMedicineEdition() {
        super();
        this.entityName = "Medicamento";
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityFromForm()
     */
    @Override
    protected void loadEntityFromForm() {
        Medicine medicineToLoad = (Medicine) this.curretnEntity;
        JDialogMedicine formToLoad = (JDialogMedicine) this.editForm;

        medicineToLoad.setName(formToLoad.getjEdtName().getText());
        medicineToLoad.setDescription(formToLoad.getjTxtDescription().getText());
        medicineToLoad.setLastUpdate(Calendar.getInstance());
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityOnForm()
     */
    @Override
    protected void loadEntityOnForm() {
        JDialogMedicine formToLoad = (JDialogMedicine) this.editForm;
        Medicine currentMedicine = (Medicine) this.curretnEntity;
        formToLoad.getjEdtName().setText(currentMedicine.getName());
        formToLoad.getjTxtDescription().setText(currentMedicine.getDescription());
    }  

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#valid()
     */
    @Override
    protected String valid() {
        JDialogMedicine formToValidate = (JDialogMedicine) this.editForm;
        Medicine medicine = (Medicine) this.curretnEntity;

        if ((formToValidate.getjEdtName().getText() == null) || (formToValidate.getjEdtName().getText().isEmpty())) {
            return "Debe ingresar el nombre del medicamento";
        }

        try {
            if (this.facadeDB.getMedicineBroker().existMedicine(formToValidate.getjEdtName().getText(), medicine.getId())) {
                return "Ya existe un medicamento con el nombre ingresado";
            }
        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            return ex.getMessage();
        }

        return null;
    }

    @Override
    protected void save() throws ExceptionDAO {
            if (this.isEditing()) {
                this.facadeDB.getMedicineBroker().update((Medicine) this.curretnEntity);
            } else {
                this.facadeDB.getMedicineBroker().insert((Medicine) this.curretnEntity);
            }
    }

    @Override
    protected void loadIconOnForm() {
         this.editForm.setIconImage(ImageUtils.buildImage("resources/icons/medic/medicine16.png"));
    }

    @Override
    protected Object createEntity() {
        return new Medicine();
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogMedicine(parent, this);
    }
}
